package com.example.jf.jfapp.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

public class ActivityManageUtil {
    /**
     * 打开的activity
     **/
    private static List<Activity> activities = new ArrayList<>();
    /**
     * 应用实例
     **/
    private static ActivityManageUtil instance;

    /**
     * 获得实例
     *
     * @return
     */
    public static ActivityManageUtil getInstance() {
        if (null == instance) {
            return instance = new ActivityManageUtil();
        } else {
            return instance;
        }
    }

    /**
     * 新建了一个activity
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            this.activities.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            this.activities.remove(activity);

        }
    }

    /**
     * 应用退出，结束所有的activity
     */
    public void exit() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
        System.exit(0);
    }

    /**
     * 关闭Activity列表中的所有Activity
     */
    public void finishActivity() {
        for (Activity activity : activities) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
